#!/bin/env python

import time
import cv2
import numpy as np

pic_path = "../resource/pic/img_002.jpg"
# windowName = pic_path

# cv2.namedWindow(windowName, cv2.WINDOW_NORMAL)
# cv2.resizeWindow(windowName, 1500, 500)
pic1 = cv2.imread(pic_path)
pic1_h, pic1_w, pic1_chn = pic1.shape
print("pic1 shape:", pic1.shape)
print("pic1 size:", pic1.size)
print("pic1 dtype:", pic1.dtype)

cv2.imshow("pic1", pic1)

# 翻转 0： 上下， 1：左右， -1：上下左右
new_pic1 = cv2.flip(pic1, -1)
cv2.imshow("flip", new_pic1)

# 旋转
new_pic1 = cv2.rotate(pic1, cv2.ROTATE_90_CLOCKWISE)
cv2.imshow("rotate", new_pic1)

# 仿射变换 - 平移
M = np.float32([[1, 0, 30], [0, 1, 30]])
new_pic1 = cv2.warpAffine(pic1, M, (pic1_w, pic1_h))
cv2.imshow("warpAffine", new_pic1)

# 仿射变换 - 旋转  
# 获取旋转变换矩阵 param: 旋转中心店，旋转角度，缩放系数
M = cv2.getRotationMatrix2D((100, 100), 45, 1)
new_pic1 = cv2.warpAffine(pic1, M, (pic1_w, pic1_h))
cv2.imshow("getRotationMatrix2D", new_pic1)

# 仿射变换 - 旋转
# 通过三个点获取旋转变换矩阵
src = np.float32([[50, 50], [150, 50], [50, 150]])
dst = np.float32([[50, 150], [50, 50], [150, 150]])
M = cv2.getAffineTransform(src, dst)
print(M.shape)
new_pic1 = cv2.warpAffine(pic1, M, (pic1_w, pic1_h))
cv2.imshow("getAffineTransform", new_pic1)

# 仿射变换 - 缩放/透视变换 (通过四个坐标变换)
# src = np.float32([[40, 40], [140, 50], [30, 160], [150, 150]])
# dst = np.float32([[0, 0], [150, 0], [0, 150], [150, 150]])
src = np.float32([[40, 40], [140, 50], [30, 160], [150, 150]])
dst = np.float32([[0, 0], [150, 0], [0, 150], [150, 150]])
M = cv2.getPerspectiveTransform(src, dst)
print(M.shape)
new_pic1 = cv2.warpPerspective(pic1, M, (pic1_w, pic1_h))
cv2.imshow("warpPerspective", new_pic1)

while True:
    if cv2.waitKey(0) == ord('q'):
        break

print('销毁窗口')
cv2.destroyAllWindows()
